<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    let treeList = [
        {
            "id": 1,
            "title": "item-1",
            "children": [
                {
                    "id": 4,
                    "title": "item-1-1",
                    "children": [
                        {
                            "id": 10,
                            "title": "item-1-1-1",
                        },
                        {
                            "id": 11,
                            "title": "item-1-1-1",
                        }
                    ]
                },
                {
                    "id": 5,
                    "title": "item-1-2",
                }
            ]
        },
        {
            "id": 2,
            "title": "item-2",
            "children": [
                {
                    "id": 6,
                    "title": "item-2-1",
                },
                {
                    "id": 7,
                    "title": "item-2-2",
                }
            ]
        },
        {
            "id": 3,
            "title": "item-3",
            "children": [
                {
                    "id": 8,
                    "title": "item-3-1",
                },
                {
                    "id": 9,
                    "title": "item-3-2",
                }
            ]
        }
    ]
    console.log("treeList", treeList);


    function flatTree(treeList, pid = null) {
        treeList = JSON.parse(JSON.stringify(treeList));
        let tree = [];
        for (let i = 0; i < treeList.length; i++) {
            let item = treeList[i];
            item.pid = pid;
            tree.push(item);
            if (item.children) {
                let childTree = flatTree(item.children, item.id);
                if(childTree.length){
                    tree.push(...childTree);
                }
                delete item.children;// 在children中数据拉平之后, 变相删除children 
            }  
        }
        tree.sort((a,b)=>a.id - b.id);
        return tree;
    }

    console.log("flatTree", flatTree(treeList));

</script>

</html>